package com.lingnan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.dto.JobApplicationDto;
import com.lingnan.dto.JobPostRecordQuery;
import com.lingnan.entity.po.JobApplication;
import com.lingnan.vo.JobApplicationVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface JobApplicationMapper extends BaseMapper<JobApplication> {
    
    /**
     * 分页查询职位投递记录
     */
    IPage<JobApplicationDto> selectJobApplicationPage(Page<JobApplicationDto> page,
                                                      @Param("query") JobPostRecordQuery query);

    /**
     * 根据HR ID查询投递记录（包含职位和求职者信息）
     */
    @Select("SELECT ja.*, jp.title as position_title, js.name as seeker_name, js.phone as seeker_phone " +
            "FROM job_application ja " +
            "LEFT JOIN job_position jp ON ja.position_id = jp.id " +
            "LEFT JOIN job_seeker js ON ja.seeker_id = js.id " +
            "WHERE jp.hr_id = #{hrId} " +
            "ORDER BY ja.apply_time DESC")
    List<JobApplicationVO> selectApplicationsByHrId(@Param("hrId") Long hrId);

    /**
     * 根据职位ID查询投递记录
     */
    @Select("SELECT ja.*, js.name as seeker_name, js.phone as seeker_phone, js.email as seeker_email " +
            "FROM job_application ja " +
            "LEFT JOIN job_seeker js ON ja.seeker_id = js.id " +
            "WHERE ja.position_id = #{positionId} " +
            "ORDER BY ja.apply_time DESC")
    List<JobApplicationVO> selectApplicationsByPositionId(@Param("positionId") Long positionId);

    /**
     * 统计HR各状态投递记录数量
     */
    @Select("SELECT ja.status, COUNT(*) as count " +
            "FROM job_application ja " +
            "LEFT JOIN job_position jp ON ja.position_id = jp.id " +
            "WHERE jp.hr_id = #{hrId} " +
            "GROUP BY ja.status")
    List<Map<String, Object>> countApplicationsByStatus(@Param("hrId") Long hrId);
}